<template>
	<view class="container">
		<view class="tui-box">
			
			<view class="tui-top tui-goods-info">
				<tui-list-cell :hover="false" :lineLeft="0">
					<view class="tui-goods-title">
						商品信息
					</view>
				</tui-list-cell>
				<block>
					<tui-list-cell :hover="false" padding="0">
						<view class="tui-goods-item">
							<image :src="goodsLogo" class="tui-goods-img"></image>
							<view class="tui-goods-center">
								<view class="tui-goods-name">{{goodsName}}</view>
								<view class="tui-goods-attr"></view>
							</view>
							<view class="tui-price-right">
								<view>￥{{goodsMoney}}</view>
							</view>
						</view>
					</tui-list-cell>
				</block>
				<tui-list-cell v-if="false" :hover="false">
					<view class="tui-padding tui-flex">
						<view>折扣信息</view>
						<view>折扣抵扣信息,下单后系统自动计算</view>
					</view>
				</tui-list-cell>
				<tui-list-cell v-if="false" :hover="false">
					<view class="tui-padding tui-flex">
						<view>累计消费</view>
						<view>该商品参与累计消费</view>
					</view>
				</tui-list-cell>
				<tui-list-cell :hover="false">
					<view class="tui-padding tui-flex">
						<view>配送费</view>
						<view>包邮商品，免配送费</view>
					</view>
				</tui-list-cell>
				<tui-list-cell :hover="false">
					<view class="tui-padding tui-flex">
						<view>购买份数</view>
						<view><tui-numberbox :value="goodsNum" @change="goodsNumChange" :min="1" :max="goodsNumMax" :step="1"></tui-numberbox></view>
					</view>
				</tui-list-cell>
				<tui-list-cell v-if="false" :arrow="hasCoupon" :hover="hasCoupon" >
					<view class="tui-padding tui-flex">
						<view>优惠券</view>
						<view :class="{'tui-color-red':hasCoupon}">{{hasCoupon?"满5减1":'没有可用优惠券'}}</view>
					</view>
				</tui-list-cell>
				<tui-list-cell v-if="false" :hover="true" :arrow="true">
					<view class="tui-padding tui-flex">
						<view>发票</view>
						<view class="tui-invoice-text">不开发票</view>
					</view>
				</tui-list-cell>
				
				<tui-list-cell :hover="false" :lineLeft="0" padding="0">
					<view class="tui-remark-box tui-padding tui-flex">
						<view>订单备注</view>
						<input type="text" class="tui-remark" placeholder="选填: 请先和商家协商一致" v-model="orderRemark" placeholder-class="tui-phcolor"></input>
					</view>
				</tui-list-cell>
			</view>
			
			<view class="tui-top">
				<tui-list-cell :last="false" :radius="1">
					<view class="tui-flex">
						<view class="tui-balance">收货地址<text class="tui-gray"></text></view>
					</view>
				</tui-list-cell>
			</view>
			
			<tui-list-cell :arrow="true" :last="true" :radius="1" @click="goMyAddressIndex()">
				<view class="tui-address">
					<view v-if="(myAddressId > 0)">
						<view class="tui-userinfo">
							<text class="tui-name">{{myAddressInfo.name}}</text> {{myAddressInfo.phone}}
						</view>
						<view class="tui-addr">
							<view v-if="(myAddressInfo.is_home == 1)" class="tui-addr-tag">默认</view>
							<!-- <view class="tui-addr-tag">公司</view> -->
							<text>{{myAddressInfo.address_all}}</text>
						</view>
					</view>
					<view class="tui-none-addr" v-else>
						<image src="/static/images/index/map.png" class="tui-addr-img" mode="widthFix"></image>
						<text>选择收货地址</text>
					</view>
				</view>
				<view class="tui-bg-img"></view>
			</tui-list-cell>
			
			
		</view>
		<view class="tui-safe-area"></view>
		<view class="tui-tabbar">
			<view class="tui-flex-end tui-color-red tui-pr-20">
				<view class="tui-black">订单金额: </view>
				<view class="tui-size-26">￥</view>
				<view class="tui-price-large">{{totalMoney}}</view>
			</view>
			<view class="tui-pr25">
				<tui-button width="250rpx" height="70rpx" type="danger" shape="circle" @click="postMyOrderAdd">确认下单</tui-button>
			</view>
		</view>

	</view>
</template>

<script>
	import tuiButton from "@/components/thorui/tui-button/tui-button"
	import tuiListCell from "@/components/thorui/tui-list-cell/tui-list-cell"
	import tuiBottomPopup from "@/components/thorui/tui-bottom-popup/tui-bottom-popup"
	import tuiNumberbox from "@/components/thorui/tui-numberbox/tui-numberbox"
	export default {
		components: {
			tuiButton,
			tuiListCell,
			tuiBottomPopup,
			tuiNumberbox
		},
		data() {
			return {
				hasCoupon: true,
				insufficient: false,
				
				myInfo: [],
				
				myAddressId: 0,		//选择的地址
				myAddressInfo: [],
				
				goodsInfo: {},
				goodsId: 0,
				goodsNum: 1,
				goodsName: '',
				goodsLogo: '',
				goodsMoney: 0,
				goodsStock: 0,
				goodsStatus: 1,
				orderRemark: '',
				goodsNumMax: 0,
				
				totalMoney: 0,
				shipType: 2,		//物流方式 1自提 2配送
			}
		},
		methods: {
			
			//
			back: function() {
				uni.navigateBack()
			},
			
			goodsNumChange: function(e) {
				this.goodsNum = e.value;
			//	this.totalMoney = Math.floor(this.goodsMoney * this.goodsNum);
				this.totalMoney = (parseFloat(this.goodsMoney) * parseFloat(this.goodsNum)).toFixed(2) ;
			},
			
			//返回首页
			goHome: function() {
				uni.switchTab({
					url: '/pages/site/index'
				});
			},
			
			goOrderAdd: function() {
				uni.navigateTo({
					url: '/pages/my-order/add?goods_id=' + this.goodsId,
				});
			},
			
			goMyAddressIndex: function() {
				uni.redirectTo({
					url: "/pages/my-address/index?goods_id=" + this.goodsId
				})
			},
			
			
			//配送方式
			doShipType: function(e) {
				if(e.detail.value == true) {
					this.shipType = 1;
				}else{
					this.shipType = 2;
				}
			},
			
			//我的优先地址
			getMyAddressBest: function(addressId) {
				uni.request({
					url: this.$apiUrl + '/api/user/addressInfo',
					header: {
						'userId': uni.getStorageSync('userId'),
						'userToken': uni.getStorageSync('userToken'),
						'shopId': uni.getStorageSync('shopId')
					},
					data: {
						'address_id': addressId,
					},
					success: (res) => {
						let resData = res.data['data'];
						let resError = res.data['error'];
						if(resError == 0) {
							this.myAddressId = resData.id;
							this.myAddressInfo = resData;
						}else{
							
							uni.setStorageSync('userAddressId', 0)
							
						//	this.tui.toast("数据请求失败~"); return;
						}
					}
				});
			},
			
			getGoodsInfo: function(goodsId) {
				uni.request({
				    url: this.$apiUrl+ '/api/goods/info?goods_id='+goodsId,
					header: {
						'userId': uni.getStorageSync('userId'),
						'userToken': uni.getStorageSync('userToken'),
						'shopId': uni.getStorageSync('shopId')
					},
				    data: {},
				    success: (res) => {
						let resData = res.data['data'];
						this.goodsInfo = resData;
						
						this.goodsName = resData.name;
						this.goodsLogo = resData.img_logo;
						this.goodsMoney = resData.money;
						this.goodsStatus = resData.status;
						
						this.totalMoney = this.goodsMoney;
						
						if(resData.user_limit > 0) {
							this.goodsNumMax = resData.user_limit;
						}else{
							this.goodsNumMax = resData.stock;
						}
						
				    }
				});
			},
			
			
			doPayOrder: function() {
				if(this.myAddressId == 0) {
					this.tui.toast("请选择需要配送的地址信息");
					return;
				}
				
				uni.request({
					url: this.$apiUrl+ '/api/my-order/prepay-address',
					header: {
						'userId': uni.getStorageSync('userId'),
						'userToken': uni.getStorageSync('userToken'),
						'shopId': uni.getStorageSync('shopId')
					},
					data: {
						prepay_id: this.prepayId,
						address_id: this.myAddressId,
					},
					success: (res) => {
						let resData = res.data['data'];
						let resErr = res.data['error'];
						if(resErr == 0) {	//信息无误去付款--跳转到付款台
							uni.reLaunch({
								url: "/pages/pay/wx-order?prepay_id=" + this.prepayId
							})
						}else{
							this.tui.toast("配送地址有误", 200);
						}
					}
				});
			},
			
			
			
			//立即购买
			postMyOrderAdd: function() {
				uni.request({
					url: this.$apiUrl+ '/api/order/addMallGroup',
					header: {
						'userId': uni.getStorageSync('userId'),
						'userToken': uni.getStorageSync('userToken'),
						'shopId': uni.getStorageSync('shopId'),
					},
					data: {
						goods_id: this.goodsId,
						goods_num: this.goodsNum,		//购买一件
						order_remark: this.orderRemark,
						ship_type: this.shipType,
						address_id: this.myAddressId,
					},
					success: (res) => {
						let resErr = res.data['error'];
						if(resErr == 0 ) {
							let prepayId = res.data['data']['prepay_id'];		//预支付订单的ID
							uni.navigateTo({
								url: '/pages/pay/order?prepay_id=' + prepayId
							});
						}else{
							let resMsg = res.data['msg'];
							this.tui.toast(resMsg, 2000, false);
						}
					}
				});
			},
			
			
			//得到我的个人信息
			getMyBaseInfo: function(){
				uni.request({
					url: this.$apiUrl + '/api/user/baseInfo',
					header: {
						'userId': uni.getStorageSync('userId'),
						'userToken': uni.getStorageSync('userToken'),
						'shopId': uni.getStorageSync('shopId'),
					},
					success: (res) => {
						let resErr = res.data['error'];
						let resMsg = res.data['msg'];
						let resCode = res.data['code'];
						if(resCode == 401) {		//用户未登录
							uni.navigateTo({ url: '/pages/my/login' });
						}else if(resCode == 200){
							let resData = res.data['data'];
							this.myInfo = resData;
						}else{
							this.tui.toast(resMsg, 2000, false);
						}
					}
				});
			},
			
		},
		
		onLoad: function(options) {
			this.goodsId = options.goods_id || 0;
			this.getGoodsInfo(this.goodsId);
			
			this.getMyBaseInfo();
			
		//	let addId = options.address_id || uni.getStorageSync('userAddressId');

			this.myAddressId = options.address_id || uni.getStorageSync('userAddressId');
			if(this.myAddressId) {
				this.getMyAddressBest(this.myAddressId);
			}
		},
	}
</script>

<style>
	.container {
		padding-bottom: 98rpx;
	}

	.tui-box {
		padding: 20rpx 0 118rpx;
		box-sizing: border-box;
	}

	.tui-address {
		min-height: 80rpx;
		padding: 10rpx 0;
		box-sizing: border-box;
		position: relative;
	}

	.tui-userinfo {
		font-size: 30rpx;
		font-weight: 500;
		line-height: 30rpx;
		padding-bottom: 12rpx;
	}

	.tui-name {
		padding-right: 40rpx;
	}

	.tui-addr {
		font-size: 24rpx;
		word-break: break-all;
		padding-right: 25rpx;
	}

	.tui-addr-tag {
		padding: 5rpx 8rpx;
		flex-shrink: 0;
		background: #EB0909;
		color: #fff;
		display: inline-flex;
		align-items: center;
		justify-content: center;
		font-size: 25rpx;
		line-height: 25rpx;
		transform: scale(0.8);
		transform-origin: 0 center;
		border-radius: 6rpx;
	}

	.tui-bg-img {
		position: absolute;
		width: 100%;
		height: 8rpx;
		left: 0;
		bottom: 0;
		background: url("") repeat;
	}

	.tui-top {
		margin-top: 20rpx;
		overflow: hidden;
	}

	.tui-goods-title {
		font-size: 28rpx;
		display: flex;
		align-items: center;
	}

	.tui-padding {
		box-sizing: border-box;
	}

	.tui-goods-item {
		width: 100%;
		padding: 20rpx 30rpx;
		box-sizing: border-box;
		display: flex;
		justify-content: space-between;
	}

	.tui-goods-img {
		width: 180rpx;
		height: 180rpx;
		display: block;
		flex-shrink: 0;
		border-radius: 6rpx;
	}

	.tui-goods-center {
		flex: 1;
		padding: 20rpx 8rpx;
		box-sizing: border-box;
	}

	.tui-goods-name {
		max-width: 310rpx;
		word-break: break-all;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
		font-size: 26rpx;
		line-height: 32rpx;
	}

	.tui-goods-attr {
		font-size: 22rpx;
		color: #888888;
		line-height: 32rpx;
		padding-top: 20rpx;
		word-break: break-all;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
	}

	.tui-price-right {
		text-align: right;
		font-size: 24rpx;
		color: #888888;
		line-height: 30rpx;
		padding-top: 20rpx;
	}

	.tui-flex {
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-size: 26rpx;
	}
	.tui-total-flex{
		justify-content: flex-end;
	}

	.tui-color-red,
	.tui-invoice-text {
		color: #E41F19;
		padding-right: 30rpx;
	}

	.tui-balance {
		font-size: 28rpx;
		font-weight: 500;
	}

	.tui-black {
		color: #222;
		line-height: 30rpx;
	}

	.tui-gray {
		color: #888888;
		font-weight: 400;
	}

	.tui-light-dark {
		color: #666;
	}

	.tui-goods-price {
		display: flex;
		align-items: center;
		padding-top: 20rpx;
	}

	.tui-size-26 {
		font-size: 26rpx;
		line-height: 26rpx;
	}

	.tui-price-large {
		font-size: 34rpx;
		line-height: 32rpx;
		font-weight: 600;
	}

	.tui-flex-end {
		display: flex;
		align-items: flex-end;
		padding-right: 0;
	}

	.tui-phcolor {
		color: #B3B3B3;
		font-size: 26rpx;
	}

	/* #ifndef H5 */
	.tui-remark-box {
		padding: 22rpx 30rpx;
	}

	/* #endif */
	/* #ifdef H5 */
	.tui-remark-box {
		padding: 26rpx 30rpx;
	}

	/* #endif */

	.tui-remark {
		flex: 1;
		font-size: 26rpx;
		padding-left: 64rpx;
	}

	.tui-scale-small {
		transform: scale(0.8);
		transform-origin: 100% center;
	}

	.tui-scale-small .wx-switch-input {
		margin: 0 !important;
	}

	/* #ifdef H5 */
	>>>uni-switch .uni-switch-input {
		margin-right: 0 !important;
	}

	/* #endif */
	.tui-tabbar {
		width: 100%;
		height: 98rpx;
		background: #fff;
		position: fixed;
		left: 0;
		bottom: 0;
		display: flex;
		align-items: center;
		justify-content: flex-end;
		font-size: 26rpx;
		box-shadow: 0 0 1px rgba(0, 0, 0, .3);
		padding-bottom: env(safe-area-inset-bottom);
		z-index: 999;
	}

	.tui-pr-30 {
		padding-right: 30rpx;
	}

	.tui-pr-20 {
		padding-right: 20rpx;
	}

	.tui-none-addr {
		height: 80rpx;
		padding-left: 5rpx;
		display: flex;
		align-items: center;
	}

	.tui-addr-img {
		width: 36rpx;
		height: 46rpx;
		display: block;
		margin-right: 15rpx;
	}


	.tui-pr25 {
		padding-right: 25rpx;
	}

	.tui-safe-area {
		height: 1rpx;
		padding-bottom: env(safe-area-inset-bottom);
	}
</style>